<?php

require_once 'config.php';
require_once 'umeng/Umeng.php';

$responseData = (array) simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'], 'SimpleXMLElement', LIBXML_NOCDATA);

$pdo = new \PDO('mysql:host=' . $mysql_config['hostname'] . ';dbname=' . $mysql_config['database'], $mysql_config['username'], $mysql_config['password']);
$pdo->exec('set names utf8');

//微信付款回调

if ($responseData['return_code'] == 'SUCCESS' && $responseData['result_code'] == 'SUCCESS') {

    $out_trade_no = $responseData['out_trade_no']; //商户订单号
    $total_fee = $responseData['total_fee']; //订单金额/分
    $trade_no = $responseData['transaction_id']; //微信订单号

    $total_amount = $total_fee / 100;

    $type = substr($out_trade_no, 0, 1);

    if ($type == $type_config['type_1']) {
        //商品
    } else if ($type == $type_config['type_2']) {
        //项目
        // 修改订单状态
        // 修改用户金额
        // 修改项目销售数量
        // 修改店铺余额
        // 写入店铺资金流水
        // 写入预约记录如果购买的时候选择了预约时间
        // 回调成功后写入消息
        //订单信息
        $order_query_sql = 'SELECT id, store_id, ctm_id, staff_id, starttime FROM da_order WHERE ord_num = :ord_num ORDER BY add_time DESC';
        $order_query_stat = $pdo->prepare($order_query_sql);
        $order_query_result = $order_query_stat->execute([':ord_num' => $out_trade_no]);
        if ($order_query_result) {
            $order_query_row = $order_query_stat->fetch(\PDO::FETCH_ASSOC);
            if ($order_query_row) {
                $order_id = $order_query_row['id'];
                $store_id = $order_query_row['store_id'];
                $ctm_id = $order_query_row['ctm_id'];
                $staff_id = $order_query_row['staff_id'];
                $starttime = $order_query_row['starttime'];

                //订单产品信息
                $ord_pro_query_sql = 'SELECT id, pro_id, number FROM da_ord_pro WHERE ord_id = :ord_id ORDER BY add_time DESC';
                $ord_pro_query_stat = $pdo->prepare($ord_pro_query_sql);
                $ord_pro_query_result = $ord_pro_query_stat->execute([':ord_id' => $order_id]);
                $ord_pro_query_row = $ord_pro_query_stat->fetch(\PDO::FETCH_ASSOC);
                $pro_id = $ord_pro_query_row['pro_id'];
                $number = $ord_pro_query_row['number'];

                //用户信息
                $custom_query_sql = 'SELECT id, custom_phone, custom_name, consume_money FROM da_custom WHERE id = :id';
                $custom_query_stat = $pdo->prepare($custom_query_sql);
                $custom_query_result = $custom_query_stat->execute([':id' => $ctm_id]);
                $custom_query_row = $custom_query_stat->fetch(\PDO::FETCH_ASSOC);
                $custom_phone = $custom_query_row['custom_phone'];
                $custom_name = $custom_query_row['custom_name'];
                $consume_money = $custom_query_row['consume_money'];

                //店铺信息
                $store_query_sql = 'SELECT id, money FROM da_store WHERE id = :id';
                $store_query_stat = $pdo->prepare($store_query_sql);
                $store_query_result = $store_query_stat->execute([':id' => $store_id]);
                $store_query_row = $store_query_stat->fetch(\PDO::FETCH_ASSOC);
                $store_money = $store_query_row['money'];


                $pdo->beginTransaction();

                $time = time();
                $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                //写入预约记录
                if ($staff_id == 0) {
                    $ctm_staff_insert_result = true;
                } else {
                    $ctm_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                    $ctm_staff_insert_sql = "INSERT INTO da_ctm_staff (staff_id, ctm_id, item_id, store_id, order_id, number, ctm_name, ctm_phone, status, add_time, start_time) VALUES ({$staff_id}, {$ctm_id}, {$pro_id}, {$store_id}, {$order_id}, 1, '{$ctm_name}', '{$custom_phone}', 3, {$time}, {$starttime})";
                    $ctm_staff_insert_result = $pdo->exec($ctm_staff_insert_sql);
                }

                //修改订单状态
                $status = $status_config['status_2'];
                if ($staff_id != 0) {
                    if ($number == 1) {
                        $status = 3;
                    }
                }
                $order_status_sql = "UPDATE da_order SET ordtype = {$ordtype_config['ordtype_5']}, status = {$status}, pay_time = {$time}, wx_trade_no = '{$trade_no}' WHERE id = {$order_id}";
                $order_status_result = $pdo->exec($order_status_sql);

                //修改用户金额
                $custom_money_sql = "UPDATE da_custom SET consume_money = consume_money + {$total_amount} WHERE id = {$ctm_id}";
                $custom_money_result = $pdo->exec($custom_money_sql);

                //修改项目销售数量
                $item_update_sql = "UPDATE da_item SET sell_count = sell_count + 1 WHERE id = {$pro_id}";
                $item_update_result = $pdo->exec($item_update_sql);

                //修改店铺余额
                $store_money_sql = "UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}";
                $store_money_result = $pdo->exec($store_money_sql);

                //写入店铺资金流水
                $flow_last_money = $store_money + $total_amount;
                $flow_insert_sql = "INSERT INTO da_moneyflow (type, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$moneyflow_config['type_1']}, {$moneyflow_config['pro_type_2']}, {$moneyflow_config['pay_method_5']}, {$store_id}, 0, {$ctm_id}, {$total_amount}, {$flow_last_money}, '{$flow_number}', {$time})";
                $flow_insert_result = $pdo->exec($flow_insert_sql);

                if ($ctm_staff_insert_result && $order_status_result && $custom_money_result && $item_update_result && $store_money_result && $flow_insert_result) {
                    $pdo->commit();
                    //回调成功后写入消息

                    //产品信息
                    $item_query_sql = 'SELECT id, item_name FROM da_item WHERE id = :id';
                    $item_query_stat = $pdo->prepare($item_query_sql);
                    $item_query_result = $item_query_stat->execute([':id' => $pro_id]);
                    $item_query_row = $item_query_stat->fetch(\PDO::FETCH_ASSOC);
                    $item_name = $item_query_row['item_name'];

                    //老板信息
                    $staff_query_sql = 'SELECT ds.id, ds.telephone FROM da_staff_store dss INNER JOIN da_staff ds ON dss.staff_id = ds.id WHERE dss.store_id = :store_id';
                    $staff_query_stat = $pdo->prepare($staff_query_sql);
                    $staff_query_result = $staff_query_stat->execute([':store_id' => $store_id]);
                    $staff_query_row = $staff_query_stat->fetch(\PDO::FETCH_ASSOC);
                    $boss_id = $staff_query_row['id'];
                    $boss_telephone = $staff_query_row['telephone'];

                    $custom_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                    $content = "【已付款】([{$custom_name}])[{$item_name}] X [{$number}]";
                    $servicenews_insert_sql = "INSERT INTO da_servicenews (titleid, otherid, content, add_time, userid, type, store_id) VALUES ('1', {$order_id}, '{$content}', '{$time}', {$boss_id}, 1, {$store_id})";
                    $servicenews_insert_result = $pdo->exec($servicenews_insert_sql);

                    $alias = $boss_telephone . $store_id;

                    /*
                    $umeng = new Umeng();
                    $umeng->sendAndroidCustomizedcast('订单消息', $content, 'kkmOA', $alias);
                    $umeng->sendIOSCustomizedcast($content, 'kkmOA', $alias, '', '', false);
                    */
                } else {
                    $pdo->rollBack();
                }
            }
        }
    } else if ($type == $type_config['type_4']) {
        //体验卡
        // 修改订单状态
        // 修改用户金额
        // 写入体验卡购买记录
        // 修改店铺余额
        // 写入店铺资金流水
        // 回调成功后写入消息
        //订单信息
        $order_query_sql = 'SELECT id, store_id, ctm_id, sharer_id, sharer_type FROM da_order WHERE ord_num = :ord_num ORDER BY add_time DESC';
        $order_query_stat = $pdo->prepare($order_query_sql);
        $order_query_result = $order_query_stat->execute([':ord_num' => $out_trade_no]);
        if ($order_query_result) {
            $order_query_row = $order_query_stat->fetch(\PDO::FETCH_ASSOC);
            if ($order_query_row) {
                $order_id = $order_query_row['id'];
                $store_id = $order_query_row['store_id'];
                $ctm_id = $order_query_row['ctm_id'];
                $sharer_id = $order_query_row['sharer_id'];
                $sharer_type = $order_query_row['sharer_type'];

                //订单产品信息
                $ord_pro_query_sql = 'SELECT id, pro_id FROM da_ord_pro WHERE ord_id = :ord_id ORDER BY add_time DESC';
                $ord_pro_query_stat = $pdo->prepare($ord_pro_query_sql);
                $ord_pro_query_result = $ord_pro_query_stat->execute([':ord_id' => $order_id]);
                $ord_pro_query_row = $ord_pro_query_stat->fetch(\PDO::FETCH_ASSOC);
                $pro_id = $ord_pro_query_row['pro_id'];

                //用户信息
                $custom_query_sql = 'SELECT id, custom_phone, custom_name, consume_money FROM da_custom WHERE id = :id';
                $custom_query_stat = $pdo->prepare($custom_query_sql);
                $custom_query_result = $custom_query_stat->execute([':id' => $ctm_id]);
                $custom_query_row = $custom_query_stat->fetch(\PDO::FETCH_ASSOC);
                $custom_phone = $custom_query_row['custom_phone'];
                $custom_name = $custom_query_row['custom_name'];
                $consume_money = $custom_query_row['consume_money'];

                //店铺信息
                $store_query_sql = 'SELECT id, money FROM da_store WHERE id = :id';
                $store_query_stat = $pdo->prepare($store_query_sql);
                $store_query_result = $store_query_stat->execute([':id' => $store_id]);
                $store_query_row = $store_query_stat->fetch(\PDO::FETCH_ASSOC);
                $store_money = $store_query_row['money'];


                $pdo->beginTransaction();

                $time = time();
                $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                //修改订单状态
                $order_status_sql = "UPDATE da_order SET ordtype = {$ordtype_config['ordtype_5']}, status = {$status_config['status_2']}, pay_time = {$time}, wx_trade_no = '{$trade_no}' WHERE id = {$order_id}";
                $order_status_result = $pdo->exec($order_status_sql);

                //修改用户金额
                $custom_money_sql = "UPDATE da_custom SET consume_money = consume_money + {$total_amount} WHERE id = {$ctm_id}";
                $custom_money_result = $pdo->exec($custom_money_sql);

                //写入体验卡购买记录
                $start_time = time();
                $end_time = $start_time + 31 * 24 * 3600;
                $expercard_custom_insert_sql = "INSERT INTO da_expercard_custom (store_id, expercard_id, custom_id, price, start_time, end_time, add_time) VALUES ({$store_id}, {$pro_id}, {$ctm_id}, {$total_amount}, {$start_time}, {$end_time}, {$start_time})";
                $expercard_custom_insert_result = $pdo->exec($expercard_custom_insert_sql);

                //修改店铺余额
                $store_money_sql = "UPDATE da_store SET money = money + {$total_amount}  WHERE id = {$store_id}";
                $store_money_result = $pdo->exec($store_money_sql);

                //写入店铺资金流水
                $flow_last_money = $store_money + $total_amount;
                $flow_insert_sql = "INSERT INTO da_moneyflow (type, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$moneyflow_config['type_1']}, {$moneyflow_config['pro_type_4']}, {$moneyflow_config['pay_method_5']}, {$store_id}, 0, {$ctm_id}, {$total_amount}, {$flow_last_money}, '{$flow_number}', {$time})";
                $flow_insert_result = $pdo->exec($flow_insert_sql);

                if ($sharer_type == 2) {
                    //写入代金券
                    //1.首先查一下店铺有没有体验卷
                    $coupon_query_sql = 'SELECT id, money FROM da_coupon WHERE type =2 and store_id= :id';
                    $coupon_query_stat = $pdo->prepare($coupon_query_sql);
                    $coupon_query_result = $coupon_query_stat->execute([':id' => $store_id]);
                    $coupon_query_row = $coupon_query_stat->fetch(\PDO::FETCH_ASSOC);
                    //2.如果有写入用户领取表  如果么有 先新增一张在领取
                    if (!empty($coupon_query_row['id'])) {
                        //写入用户领取表
                        $time = time();
                        $ctmcun_insert_sql = "INSERT INTO da_ctm_cun (ctm_id, cun_id, employ, add_time, isdel) VALUES ({$sharer_id}, {$coupon_query_row['id']}, 2, {$time}, 2)";
                        $ctmcun_insert_result = $pdo->exec($ctmcun_insert_sql);
                        //领取数量加一 
                        $coupon_number_sql = "UPDATE da_coupon SET send_number = 1 + {$coupon_query_row['send_number']}  WHERE id = {$coupon_query_row['id']}";
                        $coupon_number_result = $pdo->exec($coupon_number_sql);
                    } else {
                        //写入店铺体验卷
                        $time = time();
                        $coupon_insert_sql = "INSERT INTO da_coupon (type, store_id, coupontype_id, money, reach,mature,number,send_number,issue,add_time) VALUES (2, {$store_id}, 1, '50.00', '300.00','33068923200',0,0,1,{$time})";
                        $coupon_insert_result = $pdo->lastInsertId($coupon_insert_sql);
                        //写入用户领取表
                        $time = time();
                        $ctmcun_insert_sql = "INSERT INTO da_ctm_cun (ctm_id, cun_id, employ, add_time, isdel) VALUES ({$sharer_id}, {$coupon_insert_result}, 2, {$time}, 2)";
                        $ctmcun_insert_result = $pdo->exec($ctmcun_insert_sql);
                        //领取数量加一 
                        $coupon_number_sql = "UPDATE da_coupon SET send_number = 1 WHERE id = {$coupon_insert_result}";
                        $coupon_number_result = $pdo->exec($coupon_number_sql);
                    }
                }elseif ($sharer_type == 1) {
                    $ctmcun_insert_result=true;
                }
                if ($order_status_result && $custom_money_result && $expercard_custom_insert_result && $store_money_result && $flow_insert_result && $ctmcun_insert_result) {
                    $pdo->commit();
                    //回调成功后写入消息

                    //产品信息
                    $expercard_query_sql = 'SELECT id, name FROM da_expercard WHERE id = :id';
                    $expercard_query_stat = $pdo->prepare($expercard_query_sql);
                    $expercard_query_result = $expercard_query_stat->execute([':id' => $pro_id]);
                    $expercard_query_row = $expercard_query_stat->fetch(\PDO::FETCH_ASSOC);
                    $expercard_name = $expercard_query_row['name'];

                    //老板信息
                    $staff_query_sql = 'SELECT ds.id, ds.telephone FROM da_staff_store dss INNER JOIN da_staff ds ON dss.staff_id = ds.id WHERE dss.store_id = :store_id';
                    $staff_query_stat = $pdo->prepare($staff_query_sql);
                    $staff_query_result = $staff_query_stat->execute([':store_id' => $store_id]);
                    $staff_query_row = $staff_query_stat->fetch(\PDO::FETCH_ASSOC);
                    $boss_id = $staff_query_row['id'];
                    $boss_telephone = $staff_query_row['telephone'];

                    $custom_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                    $content = "【已付款】([{$custom_name}])[{$expercard_name}] X [{$number}]";
                    $servicenews_insert_sql = "INSERT INTO da_servicenews (titleid, otherid, content, add_time, userid, type, store_id) VALUES ('1', {$order_id}, '{$content}', '{$time}', {$boss_id}, 1, {$store_id})";
                    $servicenews_insert_result = $pdo->exec($servicenews_insert_sql);

                    $alias = $boss_telephone . $store_id;

                    /*
                    $umeng = new Umeng();
                    $umeng->sendAndroidCustomizedcast('订单消息', $content, 'kkmOA', $alias);
                    $umeng->sendIOSCustomizedcast($content, 'kkmOA', $alias, '', '', false);
                    */
                } else {
                    $pdo->rollBack();
                }
            }
        }
    } else if ($type == $type_config['type_5']) {
        //限时抢购
        // 修改订单状态
        // 修改用户金额
        // 限时抢购减少库存
        // 写入限时抢购购买记录
        // 修改店铺余额
        // 写入店铺资金流水
        // 写入预约记录如果购买的时候选择了预约时间
        // 回调成功后写入消息
        //订单信息
        $order_query_sql = 'SELECT id, store_id, ctm_id, staff_id, starttime FROM da_order WHERE ord_num = :ord_num ORDER BY add_time DESC';
        $order_query_stat = $pdo->prepare($order_query_sql);
        $order_query_result = $order_query_stat->execute([':ord_num' => $out_trade_no]);
        if ($order_query_result) {
            $order_query_row = $order_query_stat->fetch(\PDO::FETCH_ASSOC);
            if ($order_query_row) {
                $order_id = $order_query_row['id'];
                $store_id = $order_query_row['store_id'];
                $ctm_id = $order_query_row['ctm_id'];
                $staff_id = $order_query_row['staff_id'];
                $starttime = $order_query_row['starttime'];

                //订单产品信息
                $ord_pro_query_sql = 'SELECT id, pro_id, number FROM da_ord_pro WHERE ord_id = :ord_id ORDER BY add_time DESC';
                $ord_pro_query_stat = $pdo->prepare($ord_pro_query_sql);
                $ord_pro_query_result = $ord_pro_query_stat->execute([':ord_id' => $order_id]);
                $ord_pro_query_row = $ord_pro_query_stat->fetch(\PDO::FETCH_ASSOC);
                $pro_id = $ord_pro_query_row['pro_id'];
                $number = $ord_pro_query_row['number'];

                //限时抢购信息
                $miaosha_query_sql = 'SELECT id, item_id FROM da_miaosha WHERE id = :id';
                $miaosha_query_stat = $pdo->prepare($miaosha_query_sql);
                $miaosha_query_result = $miaosha_query_stat->execute([':id' => $pro_id]);
                $miaosha_query_row = $miaosha_query_stat->fetch(\PDO::FETCH_ASSOC);
                $item_id = $miaosha_query_row['item_id'];

                //用户信息
                $custom_query_sql = 'SELECT id, custom_phone, custom_name, consume_money FROM da_custom WHERE id = :id';
                $custom_query_stat = $pdo->prepare($custom_query_sql);
                $custom_query_result = $custom_query_stat->execute([':id' => $ctm_id]);
                $custom_query_row = $custom_query_stat->fetch(\PDO::FETCH_ASSOC);
                $custom_phone = $custom_query_row['custom_phone'];
                $custom_name = $custom_query_row['custom_name'];
                $consume_money = $custom_query_row['consume_money'];

                //店铺信息
                $store_query_sql = 'SELECT id, money FROM da_store WHERE id = :id';
                $store_query_stat = $pdo->prepare($store_query_sql);
                $store_query_result = $store_query_stat->execute([':id' => $store_id]);
                $store_query_row = $store_query_stat->fetch(\PDO::FETCH_ASSOC);
                $store_money = $store_query_row['money'];


                $pdo->beginTransaction();

                $time = time();
                $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                //写入预约记录
                if ($staff_id == 0) {
                    $ctm_staff_insert_result = true;
                } else {
                    $ctm_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                    $ctm_staff_insert_sql = "INSERT INTO da_ctm_staff (staff_id, ctm_id, item_id, store_id, order_id, number, ctm_name, ctm_phone, status, add_time, start_time) VALUES ({$staff_id}, {$ctm_id}, {$item_id}, {$store_id}, {$order_id}, 1, '{$ctm_name}', '{$custom_phone}', 3, {$time}, {$starttime})";
                    $ctm_staff_insert_result = $pdo->exec($ctm_staff_insert_sql);
                }

                //修改订单状态
                $status = $status_config['status_2'];
                if ($staff_id != 0) {
                    if ($number == 1) {
                        $status = 3;
                    }
                }
                $order_status_sql = "UPDATE da_order SET ordtype = {$ordtype_config['ordtype_5']}, status = {$status}, pay_time = {$time}, wx_trade_no = '{$trade_no}' WHERE id = {$order_id}";
                $order_status_result = $pdo->exec($order_status_sql);

                //修改用户金额
                $custom_money_sql = "UPDATE da_custom SET consume_money = consume_money + {$total_amount} WHERE id = {$ctm_id}";
                $custom_money_result = $pdo->exec($custom_money_sql);

                //限时抢购减少库存
                $miaosha_update_sql = "UPDATE da_miaosha SET number = number - 1, sell_count = sell_count + 1 WHERE id = {$pro_id}";
                $miaosha_update_result = $pdo->exec($miaosha_update_sql);

                //写入限时抢购购买记录
                $miaosha_custom_insert_sql = "INSERT INTO da_miaosha_custom (store_id, miaosha_id, custom_id,number, price, add_time) VALUES ({$store_id}, {$pro_id}, {$ctm_id}, {$total_amount}, {$time})";
                $miaosha_custom_insert_result = $pdo->exec($miaosha_custom_insert_sql);

                //修改店铺余额
                $store_money_sql = "UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}";
                $store_money_result = $pdo->exec($store_money_sql);

                //写入店铺资金流水
                $flow_last_money = $store_money + $total_amount;
                $flow_insert_sql = "INSERT INTO da_moneyflow (type, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$moneyflow_config['type_1']}, {$moneyflow_config['pro_type_5']}, {$moneyflow_config['pay_method_5']}, {$store_id}, 0, {$ctm_id}, {$total_amount}, {$flow_last_money}, '{$flow_number}', {$time})";
                $flow_insert_result = $pdo->exec($flow_insert_sql);

                if ($ctm_staff_insert_result && $order_status_result && $custom_money_result && $miaosha_update_result && $miaosha_custom_insert_result && $store_money_result && $flow_insert_result) {
                    $pdo->commit();
                    //回调成功后写入消息

                    //产品信息
                    $item_query_sql = 'SELECT id, item_name FROM da_item WHERE id = :id';
                    $item_query_stat = $pdo->prepare($item_query_sql);
                    $item_query_result = $item_query_stat->execute([':id' => $item_id]);
                    $item_query_row = $item_query_stat->fetch(\PDO::FETCH_ASSOC);
                    $item_name = $item_query_row['item_name'];

                    //老板信息
                    $staff_query_sql = 'SELECT ds.id, ds.telephone FROM da_staff_store dss INNER JOIN da_staff ds ON dss.staff_id = ds.id WHERE dss.store_id = :store_id';
                    $staff_query_stat = $pdo->prepare($staff_query_sql);
                    $staff_query_result = $staff_query_stat->execute([':store_id' => $store_id]);
                    $staff_query_row = $staff_query_stat->fetch(\PDO::FETCH_ASSOC);
                    $boss_id = $staff_query_row['id'];
                    $boss_telephone = $staff_query_row['telephone'];

                    $custom_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                    $content = "【已付款】([{$custom_name}])[{$item_name}] X [{$number}]";
                    $servicenews_insert_sql = "INSERT INTO da_servicenews (titleid, otherid, content, add_time, userid, type, store_id) VALUES ('1', {$order_id}, '{$content}', '{$time}', {$boss_id}, 1, {$store_id})";
                    $servicenews_insert_result = $pdo->exec($servicenews_insert_sql);

                    $alias = $boss_telephone . $store_id;

                    /*
                    $umeng = new Umeng();
                    $umeng->sendAndroidCustomizedcast('订单消息', $content, 'kkmOA', $alias);
                    $umeng->sendIOSCustomizedcast($content, 'kkmOA', $alias, '', '', false);
                    */
                } else {
                    $pdo->rollBack();
                }
            }
        }
    }

    echo 'success';
} else {
    echo 'fail';
}
?>